package com.xiafei.onenet.common;

import com.google.gson.Gson;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;

public class Http {

    public static String GET(String url, Map<String, String> header, Map<String, Object> params) {
        try {
            HttpClient client = new DefaultHttpClient();
            //发送get请求
            HttpGet get = new HttpGet();

            //设置请求参数
            URIBuilder builder = new URIBuilder(url);
            setParams(builder, params);
            get.setURI(builder.build());

            setHeader(get, header);

            HttpResponse response = client.execute(get);

            /**请求发送成功，并得到响应**/
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                /**读取服务器返回过来的json字符串数据**/
                String strResult = EntityUtils.toString(response.getEntity());

                return strResult;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String POST(String url, Map<String, String> header, Map<String, Object> params, Object body) {
        BufferedReader in;
        try {

            // 定义HttpClient
            HttpClient client = new DefaultHttpClient();

            // 实例化HTTP方法
            HttpPost post = new HttpPost();
            //设置请求参数
            URIBuilder builder = new URIBuilder(url);
            setParams(builder, params);
            post.setURI(builder.build());

            setHeader(post, header);

            // 设置请求体
            ByteArrayEntity entity = new ByteArrayEntity(new Gson().toJson(body).getBytes(HTTP.UTF_8));
            post.setEntity(entity);


            HttpResponse response = client.execute(post);
            int code = response.getStatusLine().getStatusCode();
            if (code == 200) {    //请求成功
                in = new BufferedReader(new InputStreamReader(response.getEntity()
                        .getContent(), "utf-8"));
                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");
                while ((line = in.readLine()) != null) {
                    sb.append(line + NL);
                }

                in.close();

                return sb.toString();
            } else {
                System.out.println("状态码：" + code);
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void setHeader(HttpRequest method, Map<String, String> header) {
        if (header != null) {
            // 设置请求头
            for (Iterator iter = header.keySet().iterator(); iter.hasNext(); ) {
                String name = (String) iter.next();
                String value = String.valueOf(header.get(name));
                method.setHeader(name, value);
            }
        }
    }

    public static void setParams(URIBuilder builder, Map<String, Object> params) {
        if (params != null) {
            for (Iterator iter = params.keySet().iterator(); iter.hasNext(); ) {
                String name = (String) iter.next();
                String value = String.valueOf(params.get(name));
                builder.addParameter(name, value);
            }
        }
    }
}
